import ajax from '../../libs/ajax.js';
import tools from '../../libs/tools.js';

let btnfh = document.getElementById('btnfh');
let tbdata = document.getElementById('tbdata');

let sz = [];

let cs2 = {
  pageNumber: 1,
  pageSize: 7
};
function glliuy() {
  ajax.send('/message/manage/queryAdminAllReply', cs2, (data) => {
    if (data.success) {
      sz = data.list;
      cs2 = data.page;
      showdata();
      showpage();
    }
  });
}

function showdata() {
  tbdata.innerHTML = '';
  for (let i = 0; i < sz.length; i++) {
    const et = sz[i];
    let tr = document.createElement('tr');
    //用户名
    let td = document.createElement('td');
    tr.append(td);
    td.append(et.user.nickname);

    //昵称
    td = document.createElement('td');
    tr.append(td);
    td.append(et.user.username);
    //内容
    td = document.createElement('td');
    tr.append(td);
    td.append(et.info);

    //信息最后修改时间
    td = document.createElement('td');
    tr.append(td);
    td.append(tools.FormData(et.lastupdate));

    // 操作
    td = document.createElement('td');
    tr.append(td);
    // 删除按钮
    let btn1 = document.createElement('span');
    btn1.append('删除');
    btn1.classList.add('btn', 'btn-primary', 'btn-sm', 'm-1');
    btn1.setAttribute('data-bs-toggle', 'modal');
    btn1.setAttribute('data-bs-target', '#scDialog');
    td.append(btn1);
    btn1.addEventListener('click', () => {
      let txtatitle = document.getElementById('txtatitle');
      let btnadd = document.getElementById('btnadd');
      let cs3 = {
        disableReason: txtatitle.value,
        umrid:et.umrid,
      };
      btnadd.addEventListener('click', () => {
        ajax.send('/message/manage/deletMessageReply', cs3, (data) => {
          if (data.message) {
            showToast(data.message)
          }
        });
      });
    });

    tbdata.append(tr);
  }
}

//#region 切换留言板
let btnqh = document.getElementById('btnqh');
btnqh.addEventListener('click', () => {
  location.href = './lybcz.html';
});
//#endregion

//#region 切换公告
let btngg = document.getElementById('btngg');
btngg.addEventListener('click', () => {
  location.href = './ggcz.html';
});
//#endregion

//#region 返回
btnfh.addEventListener('click', () => {
  location.href = './guanliyuan.html';
});
//#endregion

//#region 分页
let pages = document.querySelectorAll('#navpage>span');
function showpage() {
  pages[1].innerHTML = `
当前页/总页数/记录数：
${cs2.pageNumber}/${cs2.pageCount}/${cs2.total}
`;
}

pages[0].addEventListener('click', () => {
  cs2.pageNumber--;
  if (cs2.pageNumber < 1) {
    cs2.pageNumber = 1;
    return;
  }
  glliuy();
});

pages[2].addEventListener('click', () => {
  cs2.pageNumber++;
  if (cs2.pageNumber > cs2.pageCount) {
    cs2.pageNumber = cs2.pageCount;
    return;
  }
  glliuy();
});
//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

glliuy();
